package com.ruoyi.web.controller.community.cpc;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.community.domain.CpcBbsPost;
import com.ruoyi.community.service.ICpcBbsPostService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 党员论坛帖Controller
 * 
 * @author ruoyi
 * @date 2021-12-22
 */
@RestController
@RequestMapping("/community/cpc/post")
public class CpcBbsPostController extends BaseController
{
    @Autowired
    private ICpcBbsPostService cpcBbsPostService;

    /**
     * 查询党员论坛帖列表
     */
//    @PreAuthorize("@ss.hasPermi('community:post:list')")
    @GetMapping("/list")
    public TableDataInfo list(CpcBbsPost cpcBbsPost)
    {
        startPage();
        List<CpcBbsPost> list = cpcBbsPostService.selectCpcBbsPostList(cpcBbsPost);
        return getDataTable(list);
    }

    /**
     * 查询某人发布的论坛帖
     */
    @GetMapping("/getMyPost")
    public AjaxResult listByUserId()
    {
        CpcBbsPost cpcBbsPost = new CpcBbsPost();
        cpcBbsPost.setAuthorId(SecurityUtils.getUserID());
        cpcBbsPost.setCommunityId(SecurityUtils.getCommunityId());
        cpcBbsPost.setDeleteFlag(0);
        List<CpcBbsPost> list = cpcBbsPostService.selectCpcBbsPostList(cpcBbsPost);
        return AjaxResult.success(list);
    }

    /**
     * 导出党员论坛帖列表
     */
//    @PreAuthorize("@ss.hasPermi('community:post:export')")
    @Log(title = "党员论坛帖", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CpcBbsPost cpcBbsPost)
    {
        List<CpcBbsPost> list = cpcBbsPostService.selectCpcBbsPostList(cpcBbsPost);
        ExcelUtil<CpcBbsPost> util = new ExcelUtil<CpcBbsPost>(CpcBbsPost.class);
        return util.exportExcel(list, "党员论坛帖数据");
    }

    /**
     * 获取党员论坛帖详细信息
     */
//    @PreAuthorize("@ss.hasPermi('community:post:query')")
    @GetMapping(value = "/{postId}")
    public AjaxResult getInfo(@PathVariable("postId") String postId)
    {
        return AjaxResult.success(cpcBbsPostService.selectCpcBbsPostById(postId));
    }

    /**
     * 新增党员论坛帖
     */
//    @PreAuthorize("@ss.hasPermi('community:post:add')")
    @Log(title = "党员论坛帖", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CpcBbsPost cpcBbsPost)
    {
        System.out.println(cpcBbsPost.toString());
        return AjaxResult.success(cpcBbsPostService.insertCpcBbsPost(cpcBbsPost));
    }

    /**
     * 修改党员论坛帖
     */
//    @PreAuthorize("@ss.hasPermi('community:post:edit')")
    @Log(title = "党员论坛帖", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CpcBbsPost cpcBbsPost)
    {
        return toAjax(cpcBbsPostService.updateCpcBbsPost(cpcBbsPost));
    }

    /**
     * 删除党员论坛帖
     */
//    @PreAuthorize("@ss.hasPermi('community:post:remove')")
    @Log(title = "党员论坛帖", businessType = BusinessType.DELETE)
	@DeleteMapping("/{postIds}")
    public AjaxResult remove(@PathVariable String[] postIds)
    {
        return toAjax(cpcBbsPostService.deleteCpcBbsPostByIds(postIds));
    }
}
